package com.eight.util;

import org.springframework.util.StringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @Description: 获取时间工具类
 * @Author: Van.Planifolia
 * @CreateTime: 2023-02-27  16:47
 * @Version: 1.0
 */
public class TimeUtil {
    /**
     *
     * @param format 时间格式
     * @return 当前的LocalDateTime
     */
    public static LocalDateTime now(String format) {
        // 判断是否传递过来格式
        DateTimeFormatter dateTimeFormatter = null;
        if (StringUtils.hasText(format)) {
            dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        }
        // 获取当前时间
        LocalDateTime now = LocalDateTime.now();
        dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowStr = now.format(dateTimeFormatter);
        return LocalDateTime.parse(nowStr, dateTimeFormatter);
    }
}
